package com.nf.controller.home;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.SeatInfoDao;
import com.nf.dao.SeatRowVoDao;
import com.nf.dao.impl.SeatInfoDaoImpl;
import com.nf.dao.impl.SeatRowVoDaoImpl;
import com.nf.entity.SeatInfo;
import com.nf.vo.SeatRowVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 编写servlet，实现根据放映ID  查询其场次影厅的座位，并以二维数组的json格式返回
 */
@WebServlet("/api/SelectSeatsByShowIdServlet.do")
public class SelectSeatsByShowIdServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int showId= Integer.parseInt(req.getParameter("showId"));

        SeatRowVoDao dao1=new SeatRowVoDaoImpl();
        SeatInfoDao dao2=new SeatInfoDaoImpl();

        List<SeatRowVo> list1= dao1.selectByShowId(showId);

        SeatInfo[][] ss=new SeatInfo[list1.size()][];

        for(int i=0;i<list1.size();i++){
            List<SeatInfo> list2= dao2.selectByRow(list1.get(i).getSeatRow(),showId);
            ss[i]=new SeatInfo[list2.size()];

            for(int j=0;j<list2.size();j++){
                ss[i][j]= list2.get(j);
            }
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        out.print(new Gson().toJson(successJson(ss)));

        out.flush();
        out.close();
    }
}
